local Component = dofile("component.lua");
local components = dofile("components/init.lua");
local VueApp = commonlib.inherit(commonlib.gettable("commonlib.ToolBase"));

VueApp:Property("RootComponentOptions");
VueApp:Property("RootComponent");
VueApp:Property("RootElement");
VueApp:Property("Window");

function VueApp:ctor()
    self.m_components = {};

    -- 注册默认组件
    for key, value in pairs(components) do
        self:component(key, value);
    end
end

function VueApp:Init(window, component_options)
    if (type(component_options) == "string") then
        component_options = self:GetComponent(component_options);
    end
    component_options = component_options or {};
    self:SetWindow(window);
    self:SetRootComponentOptions(component_options);
    return self;
end

function VueApp:GetComponent(name)
    return self.m_components[name];
end

function VueApp:mount(el)
    self:SetRootElement(el or self:GetWindow():GetRootElement());
    self:SetRootComponent(Component:new():Init(self:GetRootComponentOptions(), self, {}, self:GetRootElement(), self:GetWindow()));
    self:GetRootElement():AddChildren(self:GetRootComponent());
end

function VueApp:component(name, component_options)
    if (type(name) == "table" and component_options == nil) then
        component_options = name;
        name = nil;
    end
    if (type(component_options) ~= "table") then return end

    name = name or component_options.name;

    if (type(name) ~= "string") then return end

    self.m_components[name] = component_options;
end

local vue = {}
function vue.createApp(window, component_options)
    return VueApp:new():Init(window, component_options);
end

return vue;